<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

require('echarts/theme/macarons') // echarts theme

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    title: {
      type: String,
      default: ''
    },
    chartProductTypeData: {
      type: Array,
      required: true
    },
    productTypeName: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartProductTypeData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');
      let chartProductTypeData = this.chartProductTypeData;
      let productTypeName = this.productTypeName;
      if (chartProductTypeData != null && chartProductTypeData != undefined && chartProductTypeData != '') {
        this.setOptions(chartProductTypeData)
      }
    },
    setOptions({expectedData, actualData} = {}) {
      let chartProductTypeData = this.chartProductTypeData;
      let typeName = this.productTypeName;
      let labelOption = {
        show: true,
        position: 'insideBottom',
        distance: 15,
        align: 'left',
        verticalAlign: 'middle',
        rotate: 90,
        formatter: '{c}  {name|{a}}',
        fontSize: 16,
        rich: {
          name: {}
        }
      };
      if (chartProductTypeData != null && chartProductTypeData != undefined && chartProductTypeData != '') {
        // let chartProductTypeData = typeData == "" ? "" : eval('(' + typeData + ')');
        // let productTypeName = typeName == "" ? "" : eval('(' + typeName + ')');
        let productTypeName = typeName == "" ? "" : typeName.split(',');
        this.chart.setOption({
          // color:['rgb(8,252,7)','rgb(255,168,0)','rgb(0,121,254)','rgb(0,255,251)'],
          color:['rgb(39,115,203)','rgb(255,168,0)','rgb(0,255,251)'],
          show: true,
          legend: {
            left: 'center',
          },
          animation: true,
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: 40,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            data: productTypeName,
            axisTick: {
              // alignWithLabel: true,
              show: false
            },
            axisLabel: {
              interval: 0,
              // rotate: productTypeName.length < 5 ? 0 : 0 - productTypeName.length * 4
              rotate: productTypeName.length < 5 ? 0 : -90
            }
          }],
          yAxis: [{
            type: 'value',
            axisTick: {
              show: false
            }
          }],
          series: [{
            name: chartProductTypeData == "" ? "" : chartProductTypeData[0].name,
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            // data: chartProductTypeData == "" ? "" : eval('(' + chartProductTypeData[0].data + ')'),
            data: chartProductTypeData == "" ? "" : chartProductTypeData[0].data,
            animationDuration
          }, {
            name: chartProductTypeData == "" ? "" : chartProductTypeData[1].name,
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            // data: chartProductTypeData == "" ? "" : eval('(' + chartProductTypeData[1].data + ')'),
            data: chartProductTypeData == "" ? "" : chartProductTypeData[1].data,
            animationDuration
          }]
          /*series: [{
            name: 'pageA',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: [79, 52, 200, 334, 390, 330, 220],
            animationDuration
          }, {
            name: 'pageB',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: [80, 52, 200, 334, 390, 330, 220],
            animationDuration
          }, {
            name: 'pageC',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: [30, 52, 200, 334, 390, 330, 220],
            animationDuration
          }]*/
        })
      }
    }
  }
}
</script>
